/***************************************************************************
 *   oboe.controllers.scripting.Interpretor                                *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.controllers.scripting
{
	import flash.utils.setTimeout;
	
	import oboe.core.Oboe;
	import oboe.core.SigSlot;
	
	public class Interpretor extends SigSlot
	{
		private var sigType:String;
		
		public function Interpretor( sigType:String="broadcastSignal" )
		{
			super();
			this.sigType = sigType;
		}
		
		[slot] public function changeFPS( fps:String ):void
		{
			Oboe.stage.frameRate = parseFloat( fps ); 
		}
		
		[slot] public function wait( time:Number, ... params ):void
		{
			var that:Interpretor = this;
			setTimeout(function():void{
				if( !that.getIsRooted() ) return; 
				trace("[SCRIPT " + time + "] " + params.join(" "));
				that[ that.sigType ].apply( null, params );
			}, time*1000 );
		}
		
		[slot] public function runCommands( cmds:Array ):void
		{
			for each( var cmdStr:String in cmds )
			{
				var cmd:Array = cmdStr.split(/('.*?'|".*?"|\S+)/g).filter(
					function(item:String, index:int, array:Array):Boolean{
						return Boolean(index&1); 
					}
				);
				for( var i:int=0; i<cmd.length; ++i )
				{
					if( /^('|")/.test( cmd[i] ) )
					{
						cmd[i] = cmd[i].substr(1, cmd[i].length-2);
					}
				}
				if( cmd[0] == "wait" )
				{
					if( Oboe.flags.DEBUG ) trace( "[SCRIPT DELAYED] " + cmd.join(" ") );
					cmd.shift();
					this.wait.apply( this, cmd );
				}
				else
				{
					if( Oboe.flags.DEBUG ) trace( "[SCRIPT] " + cmd.join(" ") );
					this[ this.sigType ].apply( this, cmd );
				}
			}
		}
	}
}